﻿// ===============================================================================
// Rule.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Rules
{
    using System;
    using EOS.Framework.Tools;

    /// <summary>
    /// An abstract class that contains information about a rule as well as a method to validate it.
    /// </summary>
    /// <remarks>
    /// This class is primarily designed to be used on a domain object to validate a business rule. In most cases, you will want to use the 
    /// concrete class SimpleRule, which just needs you to supply a delegate used for validation. For custom, complex business rules, you can 
    /// extend this class and provide your own method to validate the rule.
    /// </remarks>
    public abstract class Rule
    {
        /// <summary>
        /// The error description.
        /// </summary>
        private string description;

        /// <summary>
        /// Gets descriptive text about this broken rule. Cannot be null or
        /// empty or contains only whitespaces.
        /// </summary>
        /// <value>The error description.</value>
        public virtual string Description
        {
            get { return description; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (StringHelper.IsEmptyOrBlanksOnly(value))
                {
                    throw new ArgumentException(Properties.Resources.EmptyStringParameter, "value");
                } 
                
                description = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Rule"/> class with a specified error message.
        /// </summary>
        /// <param name="brokenDescription">A description of the rule that will be shown
        /// if the rule is broken. Cannot be null or empty or contains only whitespaces.</param>
        /// <exception cref="ArgumentNullException"><paramref name="brokenDescription"/> is
        /// null (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="brokenDescription"/> is a string
        /// of length 0 or contains only blanks.</exception>
        protected Rule(string brokenDescription)
        {
            if (brokenDescription == null)
            {
                throw new ArgumentNullException("brokenDescription");
            }

            if (StringHelper.IsEmptyOrBlanksOnly(brokenDescription))
            {
                throw new ArgumentException(Properties.Resources.EmptyStringParameter, "brokenDescription");
            }

            this.description = brokenDescription;
        }

        /// <summary>
        /// Validates that the rule has been followed.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <returns><c>true</c>, if the value is valid, otherwise false.</returns>
        public abstract bool Validate(object value);

        /// <summary>
        /// Gets a string representation of this rule.
        /// </summary>
        /// <returns>A string containing the description of the rule.</returns>
        public override string ToString()
        {
            return this.Description;
        }
    }
}
